<?php

namespace Api\Event;


/** 
 * 专题接口
 * @author Legend. <xcx_legender@qq.com>
 * @param 
 * @return 
 */

class TopicEvent extends BaseEvent 
{


	/** 
	 * 专题没有ID  使用key 作为查询
	 * @author Legend. <xcx_legender@qq.com>
	 * @param 
	 * @return 
	 */
	
	public function info($id, $is_title = false)
	{
		if (!$id)
		{
			return;
		}
		$service   = D('Topic', 'Service');
		if ($is_title)
		{
			$map['title'] = $id;
		}
		else
		{
			$map['id'] = intval($id);
		}
		return $service->where($map)->find();
	}

	/** 
	 * 获取首页推送Topic信息
	 * @author Legend. <xcx_legender@qq.com>
	 * @param 
	 * @return 
	 */
	//add param $isLimit 是否需要加个数限制 by susy<susy.liang@ilongyuan.com.cn>
	public function getHomeTopics($isLimit=true)
	{
		$service = D('Common/Topic', 'Service');
		$map['status'] = $service::STATUS_PASS;

		if($isLimit)
			$topics = $service->where($map)->order('sort desc, create_time desc')->limit(C('HOME_FOOTER_LIMIT'))->select();
		else
			$topics = $service->where($map)->order('sort desc, create_time desc')->select();

		return $topics;
	}

	/**
	*获取栏目信息
	*@author susy.liang<susy.liang@ilongyuan.com.cn>
	*/
	public function lists($query = '', $field='', $limit=0, $orderby='sort asc'){

		$service = D('Topic', 'Service');

		$map = array('status'=>$service::STATUS_PASS);
    	$query && $map = array_merge($map, $query);
    	
    	if($limit)
    		return $service->where($map)->field($field)->limit($limit)->order($orderby)->select();
    	else
    		return $service->where($map)->field($field)->order($orderby)->select();
	}

	/**
	*获取栏目分类下的视频列表
	*@author susy.liang<susy.liang@ilongyuan.com.cn>
	*/
	public function getVideos($id, $listNum=15, $timestamp=0){

		$service = D('Common/Video', 'Service');

		$order = 'create_time desc';

		$map = array();
		$map['type']		= $service::TYPE_NORMAL;
		$map['status']	= $service::STATUS_PASS;
		$map['topics']	= $id;
		$map['race_id'] = array('egt', null);//非比赛视频
		
		$timestamp && $map['create_time'] =  array('lt', (int)$timestamp);

		$lists = $service->where($map)->order($order)->limit($listNum)->getField('id', true);

		return $lists;
	}



	########### FROM 2015-09-22 10:10:24 #############
	

	public function setUserTopics($uid, $topic_ids = [])
	{
		$serv = D('Common/UserTopic', 'Service');
		$data['user_id'] =$map['user_id'] = (int)$uid;
		// $old = $serv->where($map)->select();
		// foreach ($old as $key => $value) {
		// 	event('Cache')->statis('Topic', $value['topic_id'], 'set', -1);
		// }
		$serv->where($map)->delete();
		foreach ($topic_ids as $topic_id) {
			$data['topic_id'] = (int) $topic_id;
			$res = $serv->add($data);
			// $res && event('Cache')->statis('Topic', $topic_id, 'set', 1);
		}
		return true;
	}

	protected function formats($topic_ids = [])
	{
		static $topics;
		if (!$topic_ids) return [];
		if (!$topics)
		{
			$res = $this->lists();
			$topics = array_column($res, 'title', 'id');
		}
		$data = [];
		foreach ($topic_ids as $value) {
			if (!$value || !$topics[$value]) continue;
			$data[$value] = array(
				'id'    => $value,
				'title' => $topics[$value],
			);
		}
		return $data;
	}


	public function getUserTopics($uid)
	{
		$map['user_id'] = (int)$uid;
		$res = service('UserTopic')->where($map)->order('id asc')->select();
		$topic_ids = array_column($res, 'topic_id'); 
		return $this->formats($topic_ids);
 	}

 	public function getHotTopics()
 	{
 		// $topic_ids = event('Cache')->get('Topics.hot');
 		// if (!$topic_ids)
 		// {
 		// 	$total = service('UserTopic')->total([], 'topic_id', 'topic_id');
 		// 	$data = array_column($total, 'count', 'topic_id');
 		// 	arsort($data);
 		// 	$topic_ids = array_keys($data);
 		// 	event('Cache')->set('Topics.hot', $topic_ids, 3600*24);
 		// }
 		$res = service('Topic')->where(['status' => 1 ])->order('sort asc')->field('id')->select();
 		$topic_ids = array_column($res, 'id');
		return $this->formats($topic_ids);
 	}

}